package com.eascs.dubbo.api.loanAccount;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.account.account.AccountEntity;
import com.eascs.account.api.ICapitalFlowService;
import com.eascs.account.api.constants.enums.IndividualAccountType;
import com.eascs.admin.api.loanAccount.AccountRemakeService;
import com.eascs.admin.api.loanAccount.entity.Account;
import com.eascs.admin.api.loanAccount.entity.O2oBackFee;
import com.eascs.admin.api.loanAccount.vo.ReFundVo;
import com.eascs.admin.o2o.loan.service.O2oChargeSituationService;
import com.eascs.admin.o2o.submitted.service.IO2oChargeSituationService;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UuidUtil;
import com.eascs.crm.nsEditDetail.dao.MainStoreInfoDao;
import com.eascs.demand.entity.UserDemandApplyComment;
import com.eascs.demand.service.DemandApplyCommentService;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sequence.SequenceEntity;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sequence.SequenceService;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.vo.ModelResult;
@Service
public class AccountRemakeServiceImpl implements AccountRemakeService{
    
	@Reference
	ICapitalFlowService iCapitalFlowService;
	@Autowired
	O2oChargeSituationService o2oChargeSituationService;
	@Autowired
	IO2oChargeSituationService iO2oChargeSituationService;
	@Reference
	SysOrgService sysOrgService;
	@Reference
	private SequenceService sequenceService;
	@Autowired
	MainStoreInfoDao mainStoreInfoDao;
	@Autowired
	DemandApplyCommentService demandApplyCommentService;
	@Override
	public PageResult<Object[]> amountAvailable(String accountId) {
		PageResult<Object[]> pageResult=iCapitalFlowService.individualAccountPage(accountId, IndividualAccountType.USE, 0).getModel();
		return pageResult;
	}

	@Override
	public PageResult<Object[]> freezingAmount(String accountId) {
		PageResult<Object[]> pageResult=iCapitalFlowService.individualAccountPage(accountId, IndividualAccountType.FREEZE, 0).getModel();
		return pageResult;
	}

	@Override
	public Account queryLoandedAmount(String id) {
		Account account=new Account();
		AccountEntity ccount=o2oChargeSituationService.queryByLoanId(id,null,"21");
		account.setParentId(ccount.getParentId());
		account.setBalance(ccount.getBalance());
		account.setFreezeBalance(ccount.getFreezeBalance());
		return account;
	}

	@Override
	public Account queryLoandedFrezzzed(String id) {
		Account account=new Account();
		AccountEntity ccount=o2oChargeSituationService.queryByLoanId(id,null,"22");
		account.setParentId(ccount.getParentId());
		account.setBalance(ccount.getBalance());
		account.setFreezeBalance(ccount.getFreezeBalance());
		return account;
	}

	@Override
	public BigDecimal getUsedBalance(String loanapplyNo) {
		BigDecimal balance =o2oChargeSituationService.getFreezeBalanceByLoanId(loanapplyNo); 
		return balance;
	}
    
	/**
	 * zx 生成贷款编号
	 * 
	 * @param code zx
	 * @param type
	 * @return
	 */
	private String buildCode(String code) {
		String tmp = FastDateFormat.getInstance("yyyy-MM-dd").format(new Date());
		String bill = tmp.substring(2, 4) + tmp.substring(5, 7);
		String prefix = code + "-" + bill;
		String flag = sequenceService.getSeq(SequenceEntity.O2O_LOAN_NO, 5, prefix);
		if (flag == null) {
			throw new RuntimeException();
		}
		return flag;
	}
	
	
	@Override
	public int insertRebackFund(ReFundVo reFundVo) {
		int flag=0;
		User user = UserHolder.getUser();
		O2oBackFee entity=new O2oBackFee();
		ActOrgEntity actOrgEntity = null;
		if(entity.getId()==null){
			entity.setCreateDate(new Date());
			entity.setCreateId(user.getNo());
			entity.setCreateName(user.getCname());
			entity.setStatus("NEW");
			entity.setIsDel("N");
			entity.setBillCode(buildCode("TX"));
			entity.setUniqueId(UuidUtil.generateShortUuid());
			ModelResult<ActOrgEntity>  s = sysOrgService.getUniquePlatformWithUser(user.getNo());
			if(s.isSuccess()){
				actOrgEntity = s.getModel();
			}else{
				return 0;
			}
			//actOrgEntity = sysOrgService.getOrgByUnitNo(entity.getLtd());
			if(actOrgEntity!=null){
				entity.setLtdName(actOrgEntity.getOrgName());
				entity.setLtdCode(actOrgEntity.getUnitCode());
				entity.setLtd(actOrgEntity.getUnitNo());
			}
			entity.setBackMoney(new BigDecimal(reFundVo.getBackMoney()));
			entity.setBackBank(reFundVo.getBackBank());
			entity.setBackNo(reFundVo.getBackNo());
			entity.setBackType(reFundVo.getBackType());
			entity.setBackPs(reFundVo.getBackPs());
			entity.setLoanId(reFundVo.getId());
			entity.setLoanName(reFundVo.getCustermName());
			entity.setDocumentNo(reFundVo.getIdCard());
			flag = mainStoreInfoDao.insert(entity);
	}
		return flag;
}
	@Override
	public int updateRebackFund(ReFundVo reFundVo) {
		int flag=0;
		O2oBackFee  entity = mainStoreInfoDao.getById(O2oBackFee.class, reFundVo.getId());
		entity.setBackMoney(new BigDecimal(reFundVo.getBackMoney()));
		entity.setBackBank(reFundVo.getBackBank());
		entity.setBackNo(reFundVo.getBackNo());
		entity.setBackType(reFundVo.getBackType());
		entity.setBackPs(reFundVo.getBackPs());
	    flag = mainStoreInfoDao.updateById(entity);
		return flag;
	}

	@Override
	public int delRebackFund(String id) {
		int flag=0;
		O2oBackFee  entity = mainStoreInfoDao.getById(O2oBackFee.class, id);
		entity.setIsDel("Y");
		flag=mainStoreInfoDao.updateById(entity);
		return flag;
	}

	@Override
	public List<UserDemandApplyComment> queryNode(String billcode) {
		return demandApplyCommentService.getCommentViewListByTicketId(billcode);
	}
	
}
